import uuid
import logging
logger = logging.getLogger(__name__)

class RequestIDMiddleware:
    def __init__(self, get_response):
        self.get_response = get_response

    def __call__(self, request):
        request_id = request.headers.get('RequestID')

        if not request_id:
            request_id = str(uuid.uuid4())
            request.META['HTTP_REQUESTID'] = request_id

        response = self.get_response(request)
        response['RequestID'] = request_id
        logger.info(f'RequestID: {request_id}')
        return response
